package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;

import com.itheima.service.impl.EmpServiceimpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    //    @GetMapping
//    public Result selectPage(@RequestParam(defaultValue = "1") Integer page,
//                             @RequestParam(defaultValue = "10") Integer pageSize){
//        log.info(page+" "+pageSize);
//        PageBean pageBean= empService.selectPage(page,pageSize);
//        return Result.success(pageBean);
//    }
    @GetMapping
    public Result selectPage(EmpQueryParam empQueryParam) {
        log.info("请求参数{}", empQueryParam);
        PageBean pageBean = empService.selectPage(empQueryParam);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result saveEmp(@RequestBody Emp emp){
        log.info("请求体对象{}",emp);
        empService.saveEmp(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result selectList(){
        List<Emp> list = empService.selectAll();
        return Result.success(list);
    }
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        empService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        empService.updateEmp(emp);
        return Result.success(emp);
    }
}
